unit Thrad_ExctData;

interface

uses
  Classes,Uni,AdvGrid,Class_KzThrad,IniFiles;

type
  TDataDict = class(TObject)
  public
    CODE:string;
    TYP0:Extended;
    TYP1:Extended;
    TYP2:Extended;
    TYP3:Extended;
    TYP4:Extended;
  public
    constructor Create;
  end;

  TThradExctData = class(TKzThrad)
  public
    FListData:TStringList;//&
    FListGNKM:TCollection;//&
    FGridData:TAdvStringGrid;
  public
    FListDict:TStringList;//*list of *tdatadict
  protected
    function  ToValue(aValue:Extended):string;
  protected
    procedure Execute; override;
  end;

implementation

uses
  Class_Data,Class_Dict,Class_KzUtils;


procedure TThradExctData.Execute;
var
  I,M:Integer;
  cIDX:Integer;
  cTXT:string;
  
  Data:TDATA;
  Dict:TDICT;

  {#TOTL:Extended;
  TOT1:Extended;
  TOT2:Extended;
  TOT3:Extended;
  TOT4:Extended;}
  
  TYPR:Extended;
  TYP1:Extended;
  TYP2:Extended;
  TYP3:Extended;
  TYP4:Extended;

  xDict:TDataDict;
  cGNKM:TDict;
  cDATA:TDATA;
  cHash:THashedStringList;//*
  cEror:TStringList;      //*
  Finded:Boolean;
  
begin
  cEror:=TStringList.Create;
  cHash:=THashedStringList.Create;
  for I:=0 to FListGNKM.Count-1 do
  begin
    cGNKM:=TDICT(FListGNKM.Items[I]);
    if cGNKM = nil then Continue;
    //#if cGNKM.DICTLEVL<>3 then Continue;

    cHash.Add(cGNKM.CODELINK);
  end;

  Finded:=False;
  for M:=0 to FListData.Count-1 do
  begin
    cDATA:=TDATA(FListData.Objects[M]);
    if cDATA = nil then Continue;

    if cHash.IndexOf(cDATA.DATACODE)=-1 then
    begin
      if cEror.IndexOf(cDATA.DATACODE)=-1 then
      begin
        cEror.Add(cDATA.DATACODE);
      end;
    end;
  end;

  if cEror.Count>0 then
  begin
    GetMsgProgress(Class_KzThrad.CONST_THRAD_STAT_EROR,[cEror.Text]);
    Exit;
  end;
  
  with FGridData do
  begin
    BeginUpdate;

    GetMaxProgress(FGridData.RowCount);
    for I:=1 to RowCount-1 do
    begin
      GetOneProgress(I);
      Dict:=TDICT(Objects[0,I]);
      if Dict=nil then Continue;

      //#if Dict.DICTLEVL<>3 then Continue;

      {TOTL:=0;
      TOTR:=0;
      TOT1:=0;
      TOT2:=0;
      TOT3:=0;
      TOT4:=0;}

      TYPR:=0;
      TYP1:=0;
      TYP2:=0;
      TYP3:=0;
      TYP4:=0;

      for M:=0 to FListData.Count-1 do
      begin
        Data:=TDATA(FListData.Objects[M]);
        if Data=nil then Continue;

        {TOTL:=TOTL + DATA.DATAVAL;
        if Data.DATATYPE=1 then
        begin
          TOT1:=TOT1 + Data.DATAVAL;
        end else
        if Data.DATATYPE=2 then 
        begin
          TOT2:=TOT2 + Data.DATAVAL;
        end else
        if Data.DATATYPE=3 then
        begin
          TOT3:=TOT3 + Data.DATAVAL;
        end else
        if Data.DATATYPE=4 then
        begin
          TOT4:=TOT4 + Data.DATAVAL;
        end;}
        

        if Dict.CODELINK = Data.DATACODE then
        begin
          //#TYPR:=TYPR+ Data.DATAVAL;
          
          if Data.DATATYPE=1 then
          begin
            TYP1:=TYP1 + Data.DATAVAL;
          end else
          if Data.DATATYPE=2 then 
          begin
            TYP2:=TYP2 + Data.DATAVAL;
          end else
          if Data.DATATYPE=3 then
          begin
            TYP3:=TYP3 + Data.DATAVAL;
          end else
          if Data.DATATYPE=4 then
          begin
            TYP4:=TYP4 + Data.DATAVAL;
          end;        
        end;
        
        {if TKzUtils.CompareTextLike(Dict.CODELINK,Data.DATACODE) then
        begin
          TOTR:=TOTR+ Data.DATAVAL;

          if Data.DATATYPE=1 then
          begin
            TYP1:=TYP1 + Data.DATAVAL;
          end else
          if Data.DATATYPE=2 then 
          begin
            TYP2:=TYP2 + Data.DATAVAL;
          end else
          if Data.DATATYPE=3 then
          begin
            TYP3:=TYP3 + Data.DATAVAL;
          end else
          if Data.DATATYPE=4 then
          begin
            TYP4:=TYP4 + Data.DATAVAL;
          end;
        end;}
      end;

      //#Cells[6, I]:=(ToValue(TYPR));
      Cells[7 ,I]:=(ToValue(TYP1));
      Cells[8 ,I]:=(ToValue(TYP2));
      Cells[9 ,I]:=(ToValue(TYP3));
      Cells[10,I]:=(ToValue(TYP4));

      Alignments[6, I]:=taRightJustify;
      Alignments[7 ,I]:=taRightJustify;
      Alignments[8 ,I]:=taRightJustify;
      Alignments[9 ,I]:=taRightJustify;
      Alignments[10,I]:=taRightJustify;

      //#Cells[6 ,1]:=(ToValue(0));
      Cells[7 ,1]:=(ToValue(0));
      Cells[8 ,1]:=(ToValue(0));
      Cells[9 ,1]:=(ToValue(0));
      Cells[10,1]:=(ToValue(0));
      Alignments[6 ,1]:=taRightJustify;      
      Alignments[7 ,1]:=taRightJustify;
      Alignments[8 ,1]:=taRightJustify;
      Alignments[9 ,1]:=taRightJustify;
      Alignments[10,1]:=taRightJustify;            
    end;


    GetEndProgrees;

    if FListDict =nil then
    begin
      FListDict:=TStringList.Create;
    end;
    TKzUtils.JustCleanList(FListDict);
    
    for I:=1 to RowCount-1 do
    begin
      Dict:=TDICT(Objects[0,I]);
      if Dict=nil then Continue;

      //#if Dict.DICTLEVL<>3 then Continue;

      cTXT:=Copy(Dict.CODELINK,1,3);
      cIDX:=FListDict.IndexOf(cTXT);
      
      if cIDX=-1 then
      begin
        xDict := TDataDict.Create;
        xDict.CODE:=cTXT;
        xDict.TYP0:= xDict.TYP0 + TKzUtils.TextToFloat(Cells[6,I]);
        xDict.TYP1:= xDict.TYP1 + TKzUtils.TextToFloat(Cells[7,I]);
        xDict.TYP2:= xDict.TYP2 + TKzUtils.TextToFloat(Cells[8,I]);
        xDict.TYP3:= xDict.TYP3 + TKzUtils.TextToFloat(Cells[9,I]);
        xDict.TYP4:= xDict.TYP4 + TKzUtils.TextToFloat(Cells[10,I]);

        FListDict.AddObject(xDict.CODE,xDict);
      end else
      begin
        xDict:=TDataDict(FListDict.Objects[cIDX]);

        xDict.TYP0:= xDict.TYP0 + TKzUtils.TextToFloat(Cells[6,I]);
        xDict.TYP1:= xDict.TYP1 + TKzUtils.TextToFloat(Cells[7,I]);
        xDict.TYP2:= xDict.TYP2 + TKzUtils.TextToFloat(Cells[8,I]);
        xDict.TYP3:= xDict.TYP3 + TKzUtils.TextToFloat(Cells[9,I]);
        xDict.TYP4:= xDict.TYP4 + TKzUtils.TextToFloat(Cells[10,I]);
      end;


      if Length(Dict.CODELINK)>5 then
      begin
        cTXT:=Copy(Dict.CODELINK,1,5);
        cIDX:=FListDict.IndexOf(cTXT);

        if cIDX=-1 then
        begin
          xDict := TDataDict.Create;
          xDict.CODE:=cTXT;
          xDict.TYP0:= xDict.TYP0 + TKzUtils.TextToFloat(Cells[6,I]);
          xDict.TYP1:= xDict.TYP1 + TKzUtils.TextToFloat(Cells[7,I]);
          xDict.TYP2:= xDict.TYP2 + TKzUtils.TextToFloat(Cells[8,I]);
          xDict.TYP3:= xDict.TYP3 + TKzUtils.TextToFloat(Cells[9,I]);
          xDict.TYP4:= xDict.TYP4 + TKzUtils.TextToFloat(Cells[10,I]);

          FListDict.AddObject(xDict.CODE,xDict);
        end else
        begin
          xDict:=TDataDict(FListDict.Objects[cIDX]);

          xDict.TYP0:= xDict.TYP0 + TKzUtils.TextToFloat(Cells[6,I]);
          xDict.TYP1:= xDict.TYP1 + TKzUtils.TextToFloat(Cells[7,I]);
          xDict.TYP2:= xDict.TYP2 + TKzUtils.TextToFloat(Cells[8,I]);
          xDict.TYP3:= xDict.TYP3 + TKzUtils.TextToFloat(Cells[9,I]);
          xDict.TYP4:= xDict.TYP4 + TKzUtils.TextToFloat(Cells[10,I]);
        end;      
      end;
    end;

    for I:=1 to RowCount-1 do
    begin
      Dict:=TDICT(Objects[0,I]);
      if Dict=nil then Continue;

      if Dict.DICTLEVL=3 then Continue;

      cIDX := FListDict.IndexOf(Dict.CODELINK);
      if cIDX <> -1 then
      begin
        xDict := TDataDict(FListDict.Objects[cIDX]);

        Cells[6 ,I]:=TKzUtils.FloatToText(xDict.TYP0,False,0);
        Cells[7 ,I]:=TKzUtils.FloatToText(xDict.TYP1,False,0);
        Cells[8 ,I]:=TKzUtils.FloatToText(xDict.TYP2,False,0);
        Cells[9 ,I]:=TKzUtils.FloatToText(xDict.TYP3,False,0);
        Cells[10,I]:=TKzUtils.FloatToText(xDict.TYP4,False,0);
        Alignments[6 ,I]:=taRightJustify;
        Alignments[7 ,I]:=taRightJustify;
        Alignments[8 ,I]:=taRightJustify;
        Alignments[9 ,I]:=taRightJustify;
        Alignments[10,I]:=taRightJustify;               
      end;
    end;



    for I:=0 to FListDict.Count-1 do
    begin
      xDict:=TDataDict(FListDict.Objects[I]);
      if Length(xDict.CODE)<>3 then Continue;


      Cells[6 ,1]:=TKzUtils.FloatToText(xDict.TYP0 + TKzUtils.TextToFloat(Cells[6,1]),False,0);
      Cells[7 ,1]:=TKzUtils.FloatToText(xDict.TYP1 + TKzUtils.TextToFloat(Cells[7,1]),False,0);
      Cells[8 ,1]:=TKzUtils.FloatToText(xDict.TYP2 + TKzUtils.TextToFloat(Cells[8,1]),False,0);
      Cells[9 ,1]:=TKzUtils.FloatToText(xDict.TYP3 + TKzUtils.TextToFloat(Cells[9,1]),False,0);
      Cells[10,1]:=TKzUtils.FloatToText(xDict.TYP4 + TKzUtils.TextToFloat(Cells[10,1]),False,0);
      Alignments[6 ,1]:=taRightJustify;
      Alignments[7 ,1]:=taRightJustify;
      Alignments[8 ,1]:=taRightJustify;
      Alignments[9 ,1]:=taRightJustify;
      Alignments[10,1]:=taRightJustify;          
    end;

    for I:=1 to RowCount-1 do
    begin
      TYP1:=TKzUtils.TextToFloat(Cells[7 ,I]);
      TYP2:=TKzUtils.TextToFloat(Cells[8 ,I]);
      TYP3:=TKzUtils.TextToFloat(Cells[9 ,I]);
      TYP4:=TKzUtils.TextToFloat(Cells[10,I]);
      
      Cells[6 ,I]:=TKzUtils.FloatToText(TYP1+TYP2+TYP3+TYP4,False,0);
      Alignments[6 ,I]:=taRightJustify;
    end;     
    
    EndUpdate;
  end;
end;

function TThradExctData.ToValue(aValue: Extended): string;
begin
  //#Result := TKzUtils.FloatToText(aValue / 10000);
  Result := TKzUtils.FloatToText(aValue / 10000,False,0);
  //#Result := TKzUtils.FloatToText(Trunc(aValue / 10000),False,0);
end;

{ TDataDict }

constructor TDataDict.Create;
begin
  TYP0:=0;
  TYP1:=0;
  TYP2:=0;
  TYP3:=0;
  TYP4:=0;
end;

end.
